package com.boot.main.service.impl;

import com.boot.main.entity.BcomCategory;
import com.boot.main.entity.BcomSite;
import com.boot.main.mapper.BcomCategoryMapper;
import com.boot.main.mapper.BcomSiteMapper;
import com.boot.main.response.ResponseData;
import com.boot.main.response.ResponseDataUtil;
import com.boot.main.service.IBlogCategoryService;
import com.boot.main.service.ISiteService;
import com.boot.main.utils.IsNotEmptyUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SiteService implements ISiteService {
    @Resource
    private BcomSiteMapper siteMapper;

    @Override
    public ResponseData getSites(Integer page, Integer limit, Long cid, String username){
        List<BcomSite> items = siteMapper.getSites((page-1)*limit, limit, cid, username);
        Integer count = siteMapper.getTotalCount(cid, username);
        Map map = new HashMap();
        map.put("datas",items);
        map.put("count", count);
        return ResponseDataUtil.success("获取书签成功", map);
    }

    @Override
    public ResponseData getSiteUser(Integer page, Integer limit){
        List<BcomSite> items = siteMapper.getSiteUser((page-1)*limit, limit);
        Integer count = siteMapper.getTotalCountUser();
        Map map = new HashMap();
        map.put("datas",items);
        map.put("count", count);
        return ResponseDataUtil.success("获取书签用户成功", map);
    }

    @Override
    public ResponseData getSiteById(Long id){
        BcomSite item = siteMapper.getSiteById(id);
        return ResponseDataUtil.success("获取书签详情成功", item);
    }

    @Override
    public ResponseData saveSite(BcomSite site){
        site.setCreate_time(new Date().getTime());
        Integer num = siteMapper.saveSite(site);
        if(IsNotEmptyUtil.isEmpty(num)){
            return ResponseDataUtil.error("保存书签失败", null);
        }
        return ResponseDataUtil.success("保存书签成功", null);
    }

    @Override
    public ResponseData updateSite(BcomSite site){
        site.setCreate_time(new Date().getTime());
        Integer num = siteMapper.updateSite(site);
        if(IsNotEmptyUtil.isEmpty(num)){
            return ResponseDataUtil.error("修改书签失败", null);
        }
        return ResponseDataUtil.success("修改书签成功", null);
    }

    @Override
    public ResponseData deleteSite(Long id){
        Integer num = siteMapper.deleteSite(id);
        return ResponseDataUtil.success("删除书签成功", null);
    }
}
